﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using MyAlbum.Utility;
using MyAlbum.DataAccess.LinqToSql;


namespace MyAlbum.Data.Linq
{
    public class ConfigRepository : IRepository<MyAlbum.Model.Config>
    {
        private string _connectionString;
        public ConfigRepository(string connectionString)
        {
            Assert.IsNotNull(connectionString);
            this._connectionString = connectionString;
        }

        public List<MyAlbum.Model.Config> GetItems()
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                return ImagesDB.Configs.Select(c =>
                    new MyAlbum.Model.Config()
                    {
                        Description = c.Description,
                        Name = c.Name,
                        Value = c.Value,
                        Id = c.Id.ToString()

                    }).ToList();
            }
        }
        public void AddItem(MyAlbum.Model.Config config)
        {
            Config c = new Config()
            {
                Description = config.Description,
                Value = config.Value,
                Name = config.Name
            };
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                ImagesDB.Configs.InsertOnSubmit(c);
                ImagesDB.SubmitChanges();
            }
            config.Id = c.Id.ToString();
        }

        public void UpdateItem(MyAlbum.Model.Config config)
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                Config old = ImagesDB.Configs.FirstOrDefault(c => c.Id ==int.Parse( config.Id));
                if (old != null)
                {
                    old.Name = config.Name;
                    old.Value = config.Value;
                    old.Description = config.Description;
                    ImagesDB.SubmitChanges();
                }
            }


        }


        public void DeleteItem(MyAlbum.Model.Config config)
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                Config old = ImagesDB.Configs.FirstOrDefault(c => c.Id ==int.Parse( config.Id));
                if (old != null)
                {
                    ImagesDB.Configs.DeleteOnSubmit(old);
                    ImagesDB.SubmitChanges();
                }
            }
        }


        #region IRepository<Config> Members


        public MyAlbum.Model.Config GetItemById(string id)
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                //TODO:this will not work
                return ImagesDB.Configs.Where(c => c.Id == int.Parse(id)).Select(c =>
                    new MyAlbum.Model.Config()
                    {
                        Description = c.Description,
                        Name = c.Name,
                        Value = c.Value,
                        Id = c.Id.ToString()

                    }).SingleOrDefault();
            }
        }

        #endregion
    }
}
